//拦截所有请求，添加路径前缀
axios.defaults.baseURL ='http://api-breakingnews-web.itheima.net'
 
axios.intercrptors.request.use(function(config){
    if(config.url.indexOf('/my') !==-1) {
        config.headers.Authorization = localStorage.getItem('token')
    }
    return config
},function(error) {
    return Promise.reject(error)
})

//添加响应拦截器
axios.interceptors.response.use(function(response){
    //输出响应结果
    if(response.data.message == "身份认证失败！"){
        //跳转到登录页面，销毁token
        location.href = '/login.html'
        localStorage.removeItem('token')
    }
    //返回 response
    return response
},function(error) {
    return Promise.reject(error)
}
)